package com.ds.infrastructure.hyperspace.console.model.request.containerconfig.configdata;

import com.ds.infrastructure.hyperspace.console.dto.configdata.NodesDTO;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;

/**
 * @author: yaozhendong
 * @create: 2019-09-20 16:40
 **/
@Data
@NoArgsConstructor
public class TargetEndpointForm {

    @NotNull
    private String id;
    @NotBlank
    @ApiModelProperty(value = "协议名称:WEBSOCKET, HTTP, HTTP2C, HTTP2, HTTPS, WEBSOCKETS", example = "HTTP")
    @Pattern(regexp = "^WEBSOCKET|HTTP|HTTP2C|HTTP2|HTTPS|WEBSOCKETS$", message = "支持的协议（区取大小写）：WEBSOCKET|HTTP|HTTP2C|HTTP2|HTTPS|WEBSOCKETS")
    private String protocol;
    @NotBlank
    private String host;

    private String zone;
    @NotNull
    private Integer port;

    private Integer weight;

    private Integer connectionMaxPoolSize;

    private Boolean overrideHostHeader = false;

    private String customHostHeader;

    @ApiModelProperty(value = "解析方式:RAW, DNS, K8S, K8SS", example = "RAW")
    @Pattern(regexp = "^RAW|DNS|K8S|K8SS$", message = "支持的解析方式（区取大小写）：RAW|DNS|K8S|K8SS")
    private String resolverType;

    @Pattern(regexp = "^[^/]+/[^/]+$", message = "格式不正确！支持的格式：namespace/service")
    private String servicePath;

    public static NodesDTO toDTO(TargetEndpointForm item) {
        NodesDTO nodesDTO = new NodesDTO();
        nodesDTO.setId(item.getId());
        nodesDTO.setProtocol(item.getProtocol());
        nodesDTO.setHost(item.getHost());
        nodesDTO.setZone(item.getZone());
        nodesDTO.setPort(item.getPort());
        nodesDTO.setWeight(item.getWeight());
        nodesDTO.setConnectionMaxPoolSize(item.getConnectionMaxPoolSize());
        nodesDTO.setOverrideHostHeader(item.getOverrideHostHeader());
        nodesDTO.setCustomHostHeader(item.getCustomHostHeader());
        nodesDTO.setResolverType(item.getResolverType());
        nodesDTO.setServicePath(item.getServicePath());
        return nodesDTO;
    }

    public static TargetEndpointForm toVO(NodesDTO dto) {
        TargetEndpointForm nodesFrom = new TargetEndpointForm();
        nodesFrom.setId(dto.getId());
        nodesFrom.setProtocol(dto.getProtocol());
        nodesFrom.setHost(dto.getHost());
        nodesFrom.setZone(dto.getZone());
        nodesFrom.setPort(dto.getPort());
        nodesFrom.setWeight(dto.getWeight());
        nodesFrom.setConnectionMaxPoolSize(dto.getConnectionMaxPoolSize());
        nodesFrom.setOverrideHostHeader(dto.getOverrideHostHeader());
        nodesFrom.setCustomHostHeader(dto.getCustomHostHeader());
        nodesFrom.setResolverType(dto.getResolverType());
        nodesFrom.setServicePath(dto.getServicePath());
        return nodesFrom;
    }
}
